﻿#region Using directives
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using gpsMe.Properties;
using gpsMe.Classes;
using gpsMe.Utils;
#endregion

namespace gpsMe.UI.Forms
{
    /// <summary>
    /// Forms that allows user to change some settings
    /// </summary>
    public partial class gpsMeSettingsForm : Form
    {
        #region Instance
        /// <summary>
        /// Current gpsMeSettingsForm instance
        /// </summary>
        public static gpsMeSettingsForm Instance { get; private set; }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public gpsMeSettingsForm()
        {
            Instance = this;
            InitializeComponent();
            this.Text = gpsMeProperties.GPSME_TITLE;
            this.SuspendLayout();
            foreach (object oFile in gpsMeOptimizer.Instance.LanguagesInFileSystem)
            {
                string sLanguageName = gpsMeLanguage.GetNameFromXml(oFile as string);
                if (sLanguageName != null)
                    this.languagesListBox.Items.Add(sLanguageName);
            }
            this.Resize += new EventHandler(gpsMeSettingsForm_Resize);
            this.loadLanguageButton.Text = gpsMeLanguage.Instance.LoadLanguageLabel;
            this.ResumeLayout();
            if (this.languagesListBox.Items.Count > 0)
            {
                this.languagesListBox.SelectedIndex = 0;
            }
        }
        #endregion

        #region Methods / Functions / Events

        /// <summary>
        /// Invoked when the form is resized
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void gpsMeSettingsForm_Resize(object p_oSender, EventArgs p_oEventArgs)
        {
            this.SuspendLayout();
            this.languagesListBox.Width = this.Width;
            this.languagesListBox.Height = (this.Height / 8) * 7;
            this.loadLanguageButton.Top = this.languagesListBox.Height;
            this.loadLanguageButton.Left = 0;
            this.loadLanguageButton.Height = this.Height - this.languagesListBox.Height;
            this.loadLanguageButton.Width = this.Width;
            this.ResumeLayout();
            this.Invalidate();
        }

        /// <summary>
        /// Invoked when the load map button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void loadLanguageButton_Click(object p_oSender, EventArgs p_oEventArgs)
        {
            if (this.languagesListBox.SelectedIndex >= 0)
            {
                this.Enabled = false;
                Cursor.Current = Cursors.WaitCursor;
                gpsMeLanguage.Instance.LoadLanguage(gpsMeOptimizer.Instance.LanguagesInFileSystem[this.languagesListBox.SelectedIndex] as string);
                Cursor.Current = Cursors.Default;
                this.Enabled = true;
                this.Close();
            }
            else
            {
                this.Close();
            }
        }
        #endregion
    }
}